package com.longxi.tianjintianbao.ui.presenter.equipment;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.SelectEquipmentListDetailsListener;
import com.longxi.tianjintianbao.model.equipment.EquipmentListDetails;
import com.longxi.tianjintianbao.ui.adapter.multitype.equipment.EquipmentListDetailsViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.equipment.IEquipmentListDetailsAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class EquipmentListDetailsAtPresenter extends BasePresenter<IEquipmentListDetailsAtView> {

    private MultiTypeAdapter adapter;
    private Items items;
    String typeid = "";
    String name = "";

    public EquipmentListDetailsAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            SelectEquipmentListDetailsListener listener = new SelectEquipmentListDetailsListener() {
                @Override
                public void onSuccess(EquipmentListDetails equipmentListDetails) {
//                    Intent intent = new Intent(mContext, EquipmentDetailsActivity.class);
//                    intent.putExtra(EquipmentDetailsAtPresenter.EQUIPMENTLISTDETAILS, equipmentListDetails);
//                    mContext.startActivity(intent);
                }
            };
            /* 注册类型和 View 的对应关系 */
            adapter.register(EquipmentListDetails.class, new EquipmentListDetailsViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void setBar() {
        Intent intent = mContext.getIntent();
        mContext.setTitle(intent.getStringExtra(AppConst.NAME));
        typeid = intent.getStringExtra(AppConst.TYPID);
        mContext.setTvLeft(MyUtils.getString(R.string.back));
    }

    /**
     * 测试数据
     */
    public void getEquipmentListDetails(int page){
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("typeid", typeid);
            object.put("page", page);
            object.put("rowcount", 10);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getEquipmentListDetails(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<EquipmentListDetails>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                MyUtils.showToast(throwable.message);
                mContext.showError();
            }

            @Override
            public void onNext(HttpResult<EquipmentListDetails> result) {
                items = NListRefresh.getInstance().fjData(result,
                        getView().getRefreshLayout(),
                        items,
                        adapter,
                        mContext);
            }
        });
    }

}
